<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/11/28
 * Time: 上午12:50
 */
include 'config.php';
try {
    $db = new PDO($dsn, $username, $password); // also allows an extra parameter of configuration
} catch(PDOException $e) {
    die('Could not connect to the database:<br/>' . $e);
}



function curl ($url , $data=null)
{
    $curl = curl_init();
    curl_setopt($curl , CURLOPT_URL , $url);

    if (!is_null($data)) {
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST , false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);

    curl_setopt($curl , CURLOPT_RETURNTRANSFER , 1);
    $content =curl_exec($curl);
    curl_close($curl);
    return $content;
}



$data = json_decode($_GET['data'] , true);
//var_dump($data);

# 数据不为空
if ($data) {
    $result = $db->prepare('SELECT * FROM `sm_data` WHERE `type` = :type AND `number` = :number ');
    $result->bindParam(':type',$data['type']);
    $result->bindParam(':number',$data['number']);
    $result->execute();
    $info = $result->fetchAll(PDO::FETCH_ASSOC);
    # 判断是否已经有这条开奖信息
    if (!$info  )
    {
        # 插入开奖信息
        $result = $db->prepare('INSERT INTO `sm_data` (`type` , `time` , `number` , `data`) value (:type , :time , :number , :data)');
        $result->bindParam(':type',$data['type']);
        $result->bindParam(':time',$data['time']);
        $result->bindParam(':number',$data['number']);
        $result->bindParam(':data',$data['data']);
        $result->execute();
        # 调用qq发送信息
        $result = $db->prepare('SELECT * FROM `sm_data` WHERE `type` = :type ORDER BY `time` ASC LIMIT 0,10');
        $result->bindParam(':type',$data['type']);
        $result->execute();
        $info = $result->fetchAll(PDO::FETCH_ASSOC);
        if ($info) {
            $type_arr = ['51'=>'F.T','50' => 'S.C', '1' => 'SSC' , '72' => '极速赛车'];
            $msg = '';
            $msg .= $type_arr[$info[0]['type']];
            $msg .= "\n";
            $msg .= '________________'. "\n";
            foreach ($info as $key => $value) {
                $vs = explode(',' , $value['data']);
                foreach ($vs as $kk => $vv) {
                    $vs[$kk] = substr($vv , '-1');
                }
                $vs = implode(',' , $vs);
                $msg .= '第'.substr($value['number'] , -2 ) .'起： '.$vs . "\n";
            }
            $msg .= '________________';

           echo curl('http://bocai.666it.com/send.php' , ['msg'=>$msg , 'data_id' => $info[$key]['id'] , 'data_type'=>$info[0]['type']]);
        }

    }else {
//        try {
//            $db->beginTransaction();
            // 代表存在这条开奖信息，确保其它机器人也有报奖
            $result = $db->prepare('SELECT `id` FROM `sm_data` WHERE `type` = :type ORDER BY `time` ASC LIMIT 1');
            $result->bindParam(':type',$data['type']);
            $result->execute();
            $dataInfo = $result->fetch(PDO::FETCH_ASSOC); // $dataInfo['id']
            $result = $db->prepare('SELECT `id` FROM `sm_member` WHERE `type` = 1 AND `expire_time` > :expire_time');
            $time = time();
            $result->bindParam(':expire_time',$time);
            $result->execute();
            $memberInfo = $result->fetchAll(PDO::FETCH_ASSOC);

            foreach ($memberInfo as $k1 => $v1) {
                // $v1['id'] 会员id
                // $dataInfo['id'] 数据id
                $result = $db->prepare('SELECT * FROM `sm_data_member` WHERE `data_id` = :data_id AND `member_id` = :member_id');
                $result->bindParam(':data_id',$dataInfo['id']);
                $result->bindParam(':member_id',$v1['id']);
                $result->execute();

                $hasSend = $result->fetch(PDO::FETCH_ASSOC);

                if (!$hasSend) {
                    // 代表这个机器人这个开奖没有发送
                    # 调用qq发送信息
                    $result = $db->prepare('SELECT * FROM (SELECT * FROM `sm_data` WHERE `type` = :type ORDER BY `time` DESC LIMIT 0,10) a ORDER BY `time` ASC');
                    $result->bindParam(':type',$data['type']);
                    $result->execute();
                    $info = $result->fetchAll(PDO::FETCH_ASSOC);
                    if ($info) {
                        $type_arr = ['51'=>'F.T','50' => 'S.C', '1' => 'SSC','72' => '极速赛车'];
                        $msg = '';
                        $msg .= $type_arr[$info[0]['type']].'---提示';
                        $msg .= "\n";
                        $msg .= '________________'. "\n";
                        foreach ($info as $key => $value) {
                            $vs = explode(',' , $value['data']);
                            foreach ($vs as $kk => $vv) {
                                $vs[$kk] = substr($vv , '-1');
                            }
                            $vs = implode(' ' , $vs);
                            $msg .= '第'.substr($value['number'] , -2 ) .'起： '.$vs . "\n";
                        }
                        $msg .= '______祝您好运______';

                        echo curl('http://bocai.666it.com/send.php' , ['msg'=>$msg , 'data_id' => $info[$key]['id'] , 'member_id'=>$v1['id'] , 'data_type'=>$info[0]['type']]);
                    }
                }
            }
//            $db->commit();
//        }catch (Exception $e) {
//            $db->rollBack();
//        }



    }
}




